﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using CreditAgricole.Framework;
using Microsoft.SharePoint;

namespace CreditAgricole.PageCodeBehind
{    
    /// <summary>
    /// Generate a button for adding item on a document library for the spécified list type
    /// (call Upload.aspx form)
    /// </summary>
    public class AddItemButton : WebControl
    {
        #region Fields

        private string _target = "_blank";
        private string _displayText = "Ajouter un élément";
        private Constants.ListType _listType = Constants.ListType.ProductionDeployment;
        private string _imageUrl = "/_layouts/images/CreditAgricole/AddItem.png";
        private Guid _webId = Guid.Empty;
        private string _webServerRelativeUrl;
        private string _listTitle = string.Empty;

        #endregion

        #region Properties

        public string ListTitle
        {
            get { return _listTitle; }
            set { _listTitle = value; }
        }

        public string WebServerRelativeUrl
        {
            get { return _webServerRelativeUrl; }
            set { _webServerRelativeUrl = value; }
        }

        public Guid WebId
        {
            get { return _webId; }
            set { _webId = value; }
        }

        public Constants.ListType ListType
        {
            get { return _listType; }
            set
            {
                if (_listType != value) _listType = value;
            }
        }

        public string DisplayText
        {
            get { return _displayText; }
            set
            {
                if (_displayText != value) _displayText = value;
            }
        }

        public string Target
        {
            get { return _target; }
            set
            {
                if (_target != value) _target = value;
            }
        }

        public string ImageUrl
        {
            get { return _imageUrl; }
            set
            {
                if (_imageUrl != value) _imageUrl = value;
            }
        }

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Panel buttPopup = new Panel();
            buttPopup.Width = 20;
            buttPopup.Height = 15;
            buttPopup.CssClass = "popupAddItem";
            Controls.Add(buttPopup);
            if (WebId != Guid.Empty)
            {
                string addurl = string.Empty;
                Guid siteId = SPContext.Current.Site.ID;
                using (SPSite site = new SPSite(siteId))
                {
                    using (SPWeb web = site.OpenWeb(WebId))
                    {
                        Guid listID = web.Lists.TryGetList(ListTitle).ID;
                        string separ = WebServerRelativeUrl.EndsWith("/") ? string.Empty : "/";
                        addurl = string.Format(@"{0}{1}_layouts/Upload.aspx?List={2}&RootFolder=", web.ServerRelativeUrl, separ, listID.ToString("B"));
                    }
                }
                Panel pnlButton = new Panel();
                Image imgAdd = new Image() { ImageUrl = _imageUrl, AlternateText = "+" };
                HyperLink hlAdd = new HyperLink() { NavigateUrl = addurl, Target = _target, Text = _displayText };
                LiteralControl NBSP = new LiteralControl(@"&nbsp;");
                pnlButton.Controls.Add(imgAdd);
                pnlButton.Controls.Add(NBSP);
                pnlButton.Controls.Add(hlAdd);

                Controls.Add(pnlButton);
            }
        }

        #endregion
    }
}
